package com.system.api.controller;

import com.system.api.model.Info;
import com.system.api.model.Response;
import com.system.api.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@CrossOrigin(origins = "*", maxAge=3600)
@RestController
public class ApiController {

    @Autowired
    private DataService dataService;

    @GetMapping("getYmtInfo")
    public Response getYmtInfo(){
        Response response = new Response();
        response = successResponse(response);
        response.setData(new Info("getYmtInfo",dataService.getYmtInfo()));
        return response;
    }

    @GetMapping("/getByStartEnd")
    public Response getByStartEnd(@RequestParam String cateName){
        Response response = new Response();
        response = successResponse(response);

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String start = formatter.format(date);

        String[] parts = start.split("-");

        int today = Integer.parseInt(parts[2]);
        String end = "00";
        if((today-4)<10){
            end = parts[0]+"-"+parts[1]+"-0"+String.valueOf(today-4);
        }else {
            end = parts[0]+"-"+parts[1]+"-"+String.valueOf(today-4);
        }
        response.setData(new Info("getByStartEnd",dataService.getByStartEnd(end,start,cateName)));
        return response;
    }

    @GetMapping("/getOriginByStartEnd")
    public Response getOriginByStartEnd(@RequestParam String cateName,@RequestParam String origin){
        Response response = new Response();
        response = successResponse(response);

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String start = formatter.format(date);

        String[] parts = start.split("-");

        int today = Integer.parseInt(parts[2]);
        String end = "00";
        if((today-4)<10){
            end = parts[0]+"-"+parts[1]+"-0"+String.valueOf(today-4);
        }else {
            end = parts[0]+"-"+parts[1]+"-"+String.valueOf(today-4);
        }
        response.setData(new Info("getOriginByStartEnd",dataService.getOriginByStartEnd(end,start,cateName,origin)));
        return response;
    }

    @GetMapping("/getAllOrigin")
    public Response getAllOrigin(@RequestParam String cateName){
        Response response = new Response();
        response = successResponse(response);

//        Date date = new Date();
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//        String marketDate = formatter.format(date);

        Date today = new Date();//获取今天的日期
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date yester = c.getTime();
        SimpleDateFormat yesterday=new SimpleDateFormat("yyyy-MM-dd");
        String marketDate = yesterday.format(yester);

        response.setData(new Info("getAllOrigin", dataService.getAllOrigin(marketDate,cateName)));
        return response;
    }

    @GetMapping("/getByDateCate")
    public Response getByDateCate(@RequestParam String cate){
        Response response = new Response();
        response = successResponse(response);

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String marketDate = formatter.format(date);

        response.setData(new Info("getByDateCate", dataService.getByDateCate(marketDate,cate)));
        return response;
    }

    @GetMapping("/getSiChuanJiangXi")
    public Response getSiChuanJiangXi(@RequestParam String cateName,@RequestParam String province){
        Response response = new Response();
        response = successResponse(response);

//        Date date = new Date();
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//        String marketDate = formatter.format(date);

        Date today = new Date();//获取今天的日期
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date yester = c.getTime();
        SimpleDateFormat yesterday=new SimpleDateFormat("yyyy-MM-dd");
        String marketDate = yesterday.format(yester);

        response.setData(new Info("getSiChuanJiangXi",dataService.getSiChuanJiangXi(marketDate, cateName, province)));
        return response;
    }

    @GetMapping("/getBySpecies")
    public Response getBySpecies(@RequestParam String species){
        Response response = new Response();
        response = successResponse(response);

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String marketDate = formatter.format(date);

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date yester = c.getTime();
        SimpleDateFormat yesterday=new SimpleDateFormat("yyyy-MM-dd");
        String beforeMarketDate = yesterday.format(yester);

        response.setData(new Info("getBySpecies", dataService.getBySpecies(marketDate, species)));

        if(response.getData().getResult().size()==0){
            response.setData(new Info("getBySpecies", dataService.getBySpecies(beforeMarketDate,species)));
        }

        return response;
    }

    @GetMapping("/getHotByBuyer")
    public Response getHotByBuyer(){
        Response response = new Response();
        response = successResponse(response);

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String marketDate = formatter.format(date);

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date yester = c.getTime();
        SimpleDateFormat yesterday=new SimpleDateFormat("yyyy-MM-dd");
        String beforeMarketDate = yesterday.format(yester);

        response.setData(new Info("getHotByBuyer", dataService.getHotByBuyer(marketDate)));

        if(response.getData().getResult().size()==0){
            response.setData(new Info("getHotByBuyer", dataService.getHotByBuyer(beforeMarketDate)));
        }

        return response;
    }

    @GetMapping("/getHotBySeller")
    public Response getHotBySeller(){
        Response response = new Response();
        response = successResponse(response);

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String marketDate = formatter.format(date);

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, -1);
        Date yester = c.getTime();
        SimpleDateFormat yesterday=new SimpleDateFormat("yyyy-MM-dd");
        String beforeMarketDate = yesterday.format(yester);

        response.setData(new Info("getHotBySeller", dataService.getHotBySeller(marketDate)));

        if(response.getData().getResult().size()==0){
            response.setData(new Info("getHotBySeller", dataService.getHotBySeller(beforeMarketDate)));
        }


        return response;
    }

    @GetMapping("/getNonewInfo")
    public Response getNonewInfo(){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getNonewInfo", dataService.getNonewInfo()));
        return response;
    }

    @GetMapping("/getNjwInfo")
    public Response getNjwInfo(){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getNjwInfo", dataService.getNjwInfo()));
        return response;
    }

    @GetMapping("/getAgriNyywInfo")
    public Response getAgriNyywInfo(){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getAgriNyywInfo", dataService.getAgriNyywInfo()));
        return response;
    }

    @GetMapping("/getAgriNyywArtical")
    public Response getAgriNyywArtical(@RequestParam String title){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getAgriNyywArtical", dataService.getAgriNyywArtical(title)));
        return response;
    }

    @GetMapping("/getByDateOriginSpecies")
    public Response getByDateOriginSpecies(@RequestParam String marketDate,@RequestParam String origin,@RequestParam String species){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getByDateOriginSpecies", dataService.getByDateOriginSpecies(marketDate,origin,species)));
        return response;
    }

    @GetMapping("/getByDateOriginCate")
    public Response getByDateOriginCate(@RequestParam String marketDate,@RequestParam String origin,@RequestParam String cate){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getByDateOriginSpecies", dataService.getByDateOriginCate(marketDate,origin,cate)));
        return response;
    }

    @GetMapping("/getAgriNyywInfoByProvince")
    public Response getAgriNyywInfoByProvince(@RequestParam String province){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getAgriNyywInfoByProvince", dataService.getAgriNyywInfoByProvince(province)));
        return response;
    }

    @GetMapping("/getChnAgriNews")
    public Response getChnAgriNews(){
        Response response = new Response();
        response = successResponse(response);

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String today = formatter.format(date);

        response.setData(new Info("getChnAgriNews", dataService.getChnAgriNews(today)));
        return response;
    }

    @GetMapping("/getChnAgriArticle")
    public Response getChnAgriArticle(@RequestParam String title){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getChnAgriArticle", dataService.getChnAgriArticle(title)));
        return response;
    }

    @GetMapping("/getZcfgInfo")
    public Response getZcfgInfo(){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getZcfgInfo", dataService.getZcfgInfo()));
        return response;
    }

    @GetMapping("/getZcfgArticle")
    public Response getZcfgArticle(@RequestParam String title){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getZcfgArticle", dataService.getZcfgArticle(title)));
        return response;
    }

    @GetMapping("/getJingDongProducts")
    public Response getJingDongProducts(){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getJingDongProducts", dataService.getJingDongProducts()));
        return response;
    }

    @GetMapping("/getJingDongOrderChoose")
    public Response getJingDongOrderChoose(){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getJingDongOrderChoose", dataService.getJingDongOrderChoose()));
        return response;
    }

    @GetMapping("/getJingDongComment")
    public Response getJingDongComment(@RequestParam String orderChoose){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getJingDongComment", dataService.getJingDongComment(orderChoose)));
        return response;
    }

    @GetMapping("/getJingDongFrequency")
    public Response getJingDongFrequency(@RequestParam String variety){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getJingDongFrequency", dataService.getJingDongFrequency(variety)));
        return response;
    }

    @GetMapping("/getOrderByMarketDate")
    public Response getOrderByMarketDate(){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getOrderByMarketDate", dataService.getOrderByMarketDate()));
        return response;
    }

    @GetMapping("/getMaxPriceOrigin")
    public Response getMaxPriceOrigin(@RequestParam String marketDate,@RequestParam String cate){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getMaxPriceOrigin", dataService.getMaxPriceOrigin(marketDate,cate)));
        return response;
    }

    @GetMapping("/getMinPriceOrigin")
    public Response getMinPriceOrigin(@RequestParam String marketDate,@RequestParam String cate){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getMinPriceOrigin", dataService.getMinPriceOrigin(marketDate,cate)));
        return response;
    }

    @GetMapping("/getNywProductsInfoByCateDate")
    public Response getNywProductsInfoByCateDate(@RequestParam String cate){
        Response response = new Response();
        response = successResponse(response);

        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String today = formatter.format(date);

        response.setData(new Info("getNywProductsInfoByCateDate", dataService.getNywProductsInfoByCateDate(cate,today)));
        return response;
    }

    @GetMapping("/getCommentSentimentByText")
    public Response getCommentSentimentByText(@RequestParam String text){
        Response response = new Response();
        response = successResponse(response);

        response.setData(new Info("getCommentSentimentByText", dataService.getCommentSentimentByText(text)));
        return response;
    }

    private Response successResponse(Response response) {
        response.setCode(0);
        response.setMsg("success");
        return response;
    }

    private Response failResponse(Response response, String msg) {
        response.setCode(1);
        response.setMsg(msg);
        return response;
    }



}
